package com.edu.internship.busi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.internship.busi.dto.CardDTO;
import com.edu.internship.busi.entity.Card;
import com.edu.internship.busi.entity.Dictionary;
import com.edu.internship.busi.entity.DictionaryOption;
import com.edu.internship.busi.mapper.CardMapper;
import com.edu.internship.busi.service.ICardService;
import com.edu.internship.config.cache.GuavaCache;
import com.edu.internship.glabal.constants.Constants;
import com.edu.internship.glabal.constants.ResultCodeEnum;
import com.edu.internship.glabal.exception.BusinessException;
import com.edu.internship.util.BeanExUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @author LPCLYP
 * @date
 */
@Service
@Slf4j
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements ICardService {

    @Resource
    private GuavaCache guavaCache;

    /**
     * nonExistsAndConvert方法
     * @param dto
     * @return
     */
    private Card nonExistsAndConvert(CardDTO dto) {
        LambdaQueryWrapper<Card> qw = Wrappers.lambdaQuery();
        qw.eq(Card::getCardId,dto.getCardId());
        if (super.count(qw)>0) {
            throw new BusinessException(ResultCodeEnum.DATABASE_ERROR);
        }
        return BeanExUtils.convertTo(dto,Card::new);
    }

    /**
     * 新增
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void createCard(CardDTO dto) {
        Card card = this.nonExistsAndConvert(dto);
        card.setCardTime(LocalDateTime.now());
        //card.setCardHandingTime(LocalDateTime.now());
        super.save(card);
    }

    /**
     * 更新卡信息
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void updateCard(CardDTO dto) {

    }

    /**
     * 更新卡信息
     * @param card
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void updateCard(Card card) {
        super.updateById(card);
    }

    /**
     * 根据主键列表批量删除
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void removeCard(List<String> ids) {
        List<Card> cards = new ArrayList<>(super.listByIds(ids));
        super.removeByIds(ids);
    }
}
